$(function(){
	//页面初始化，我的产品列表
	myProductList();
	
	//我的产品绑定事件
	$('#my_product').on('click',function(){
		myProductList();
	})
	
	//待续费产品
	searchRenewOrder();
}) 

//我的产品查询
function myProductList(){
	$.ajax({
        type: "post",
        url: "/consume/order/queryList",
        dataType : "json",
        success: function(data){
        	$("#orderTab").html();
        	var myProductList = data.orderList;
        	var html = '<tr><th>产品名称</th><th>开通时间</th><th >到期时间</th><th>状态</th><th>操作</th></tr>';
        	for (var i = 0; i < myProductList.length; i++) {
				html+='<tr><td class="alt">'+myProductList[i].goodsName+'</td><td class="alt">'
				+toDate(myProductList[i].payTime,"yyyy-MM-dd hh:mm:ss")+'</td><td class="alt">'
				+toDate(myProductList[i].overTime,"yyyy-MM-dd hh:mm:ss")+'</td><td class="alt">'
				+orderStateCheck(myProductList[i].orderState)+'</td><td class="alt"><a href="/consume/order/order_detail?orderId='+myProductList[i].orderId+'" class="viewDetails" style="color:#fff;">查看详情</a></td></tr>'
			}
        	$("#orderTab").html(html);
        }
    });
}

//查询待续费订单数据（7天之内到期的）
function searchRenewOrder() {
	//待续费产品绑定事件
	$('#wait_renew').on('click',function(){
		var overTime = AddDays(new Date(),7,'day').Format('yyyy-MM-dd');
		$.ajax({
			type : "post",
			url : "/myservice/order/renewManage/queryList",
			async : false,
			data : {
				overTime : overTime
			},
			dataType : "json",
			success : function(data){
				$('#orderTab').html();
				//新增内容
				var tb_content = '<tr><th>订单编号</th><th>商品名称</th><th>到期时间</th><th>开通状态</th><th>操作</th></tr>';
				if(data.orders.length > 0) {
					$.each(data.orders,function(index,order){
						tb_content += '<tr>'
							 	   + '<td class="alt">'+order.orderNum+'</td>' 
								   + '<td class="alt">'+order.goodsName+'</td>'
								   + '<td class="alt time">'+toDate(order.overTime,"yyyy-MM-dd hh:mm")+'</td>'
								   + '<td class="alt">'+checkIsDredge(order.isDredge)+'</td>'
								   + '<td class="alt">'+checkDoState(order.orderId,order.isDredge)+'</td>'
								   + '</tr>';
					})
				}else {
					tb_content += '<tr><td colspan="5">没有符合条件的订单，请尝试其他搜索条件。</td></tr>';
				}
				$('#orderTab').html(tb_content);
			}
		});
	})
}

//判断操作显示
function checkDoState(orderId,isDredge) {
	var str = '';
	if(isDredge == null || isDredge == '' || isDredge == 0){
		str = '<a href="javascript:void(0)" class="viewDetails" style="color:#fff;" onclick="dredge('+orderId+')">开通</a>';
	}else{
		str = '<a href="javascript:void(0)" class="viewDetails" style="color:#fff;" onclick="renew('+orderId+')">续费</a>';
	}
	return str;
}

//判断是否开通
function checkIsDredge(isDredge) {
	var str = '';
	if(isDredge == null || isDredge == '' || isDredge == 0){
		str = '未开通';
	}else{
		str = '已开通';
	}
	return str;
}

function orderStateCheck(orderState){
	if(orderState==null||orderState==''){
		return '';
	}else{
		if(orderState == 1){
			return '已付款';
		}else if(orderState == 0){
			return '待付款';
		}else if(orderState == 2){
			return '已作废';
		}
	}
}

//续费
function renew(orderId){
	//校验是否可以续费
	$.ajax({
		type : "post",
		url : "/myservice/order/checkOrderRenew?orderId="+orderId,
		async : false,
		dataType : "json",
		success : function(data) {
			if(data) {
				window.open('/myservice/order/toRenewPage?orderId='+orderId);
			}else {
				BSTYUN.dialog.warn("当前使用时间已达到上线，如需继续续费，请先续费该产品加入的套餐！");
			}
		}
	})
}

//开通应用
function dredge(orderId){
	$.ajax({
		type : "post",
		url : "/eos/dredgeApply?orderId="+orderId,
		async : false,
		dataType : "json",
		success : function(data){
			if(data) {
				searchOrder();
			}else {
				BSTYUN.dialog.warn("网络异常，开通失败，请重试！");
			}
		}
	})
}

